package com.yyb.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import com.yyb.common.PageInfo;
import com.yyb.domain.TbDorm;
import com.yyb.dto.TbDormAddDTO;
import com.yyb.dto.TbDormUpdateDTO;
import com.yyb.dto.TbDormQueryDTO;
import com.yyb.mapper.TbDormMapper;
import com.yyb.service.TbDormService;
import com.yyb.vo.TbDormVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TbDormServiceImpl implements TbDormService {

    private final TbDormMapper dormMapper;

    // ========== 新增 ==========
    @Override
    @Transactional
    public TbDormVO addDorm(TbDormAddDTO dto) {
        TbDorm entity = new TbDorm();
        BeanUtils.copyProperties(dto, entity);
        entity.setUpdateTime(new Date());
        dormMapper.insert(entity);
        return toVO(entity);
    }

    // ========== 修改 ==========
    @Override
    @Transactional
    public TbDormVO updateDorm(TbDormUpdateDTO dto) {
        TbDorm entity = dormMapper.selectByPrimaryKey(dto.getId());
        if (entity == null) {
            throw new IllegalArgumentException("数据不存在");
        }
        BeanUtils.copyProperties(dto, entity);
        entity.setUpdateTime(new Date());
        dormMapper.updateByPrimaryKey(entity);
        return toVO(entity);
    }

    // ========== 删除 ==========
    @Override
    @Transactional
    public void deleteDorm(Long id) {
        dormMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void deleteDormBatch(List<Long> ids) {
        ids.forEach(dormMapper::deleteByPrimaryKey);
    }

    // ========== 查询 ==========
    @Override
    public TbDormVO getDorm(Long id) {
        TbDorm entity = dormMapper.selectByPrimaryKey(id);
        return entity == null ? null : toVO(entity);
    }

    /** 关键分页方法 */
    @Override
    public PageInfo<TbDormVO, TbDormQueryDTO> listDormPage(TbDormQueryDTO dto) {

        // 1) PageHelper 开启分页
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        // 2) 查询
        List<TbDorm> entityList = dormMapper.selectByCondition(dto);

        // 3) PageHelper 计算分页元数据
        com.github.pagehelper.PageInfo<TbDorm> helperInfo =
                new com.github.pagehelper.PageInfo<>(entityList);

        // 4) 封装成你的 PageInfo<T, Q>
        PageInfo<TbDormVO, TbDormQueryDTO> page = new PageInfo<>();
        page.setPageNumber(helperInfo.getPageNum());
        page.setPageSize(helperInfo.getPageSize());
        page.setTotalCount(helperInfo.getTotal());  // 自动回写 totalPage
        page.setList(
                entityList.stream()
                        .map(this::toVO)
                        .collect(Collectors.toList())
        );
        page.setParams(dto);

        return page;
    }

    /** Entity → VO 转换 */
    private TbDormVO toVO(TbDorm entity) {
        TbDormVO vo = new TbDormVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}